<template>
  <div class="bg-white min-h-full h-auto">
    <el-form
      :inline="true"
      :model="form"
      label-width="160px"
      class="mt-5"
    >
      <div>
        <el-form-item label="融资额度：">
          <el-input
            v-model="form.money"
            class="w-20 mr-2.5"
          ></el-input>
          (单位：元)
        </el-form-item>
        <el-form-item label="利息：" label-width="85px">
          <el-input
            v-model="form.rate"
            class="w-20 mr-2.5"
          ></el-input>
          %
        </el-form-item>
        <el-form-item label="还本付息时间：" label-width="177px">
          <el-input
            v-model="form.repayment"
            class="w-20 mr-2.5"
          ></el-input>
          (单位：月)
        </el-form-item>
      </div>

      <div>
        <el-form-item label="申请人姓名：">
          <el-input v-model="form.realName" class="w-20"></el-input>
        </el-form-item>
        <el-form-item label="申请人手机号：">
          <el-input v-model="form.phone" class="w-30"></el-input>
        </el-form-item>
        <el-form-item label="申请人身份证号：">
          <el-input v-model="form.idNum" class="w-45"></el-input>
        </el-form-item>
      </div>

      <div v-if="form.combinationName1 != null">
        <el-form-item label="组合贷款人1姓名：">
          <el-input
            v-model="form.combinationName1"
            class="w-20"
          ></el-input>
        </el-form-item>
        <el-form-item label="组合贷款人1手机号：">
          <el-input
            v-model="form.combinationPhone1"
            class="w-30"
          ></el-input>
        </el-form-item>
        <el-form-item label="组合贷款人1身份证号：">
          <el-input
            v-model="form.combinationIdnum1"
            class="w-45"
          ></el-input>
        </el-form-item>
      </div>

      <div v-if="form.combinationName2 != null">
        <el-form-item label="组合贷款人2姓名：">
          <el-input
            v-model="form.combinationName2"
            class="w-20"
          ></el-input>
        </el-form-item>
        <el-form-item label="组合贷款人2手机号：">
          <el-input
            v-model="form.combinationPhone2"
            class="w-30"
          ></el-input>
        </el-form-item>
        <el-form-item label="组合贷款人2身份证号：">
          <el-input
            v-model="form.combinationIdnum2"
            class="w-45"
          ></el-input>
        </el-form-item>
      </div>

      <el-form-item class="pl-7">
        <el-button type="info" @click="showInfo">查看上传资料</el-button>
      </el-form-item>

      <el-dialog
        title="资料"
        v-model="show"
        width="580px"
        :before-close="closeInfo"
      >
        <el-form
          v-for="(item, index) in form.file"
          :key="index"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item prop="consignee">
            <img 
              :src="'http://localhost:9091/file/info/' + item" 
              alt="" 
              class="w-7/10 h-7/10 float-left mr-5 bg-cover" 
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <el-button type="success" @click="closeInfo">确 定</el-button>
        </template>
      </el-dialog>
    </el-form>

    <div class="flex justify-start ml-[540px]">
      <el-button
        type="danger"
        @click="handleApply('1')"
        v-show="form.status == 0 || form.status == 2"
      >
        驳回
      </el-button>
      <el-button
        type="success"
        @click="handleApply('2')"
        v-show="form.status == 0 || form.status == 1"
      >
        通过
      </el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'

import { apiClient } from "../api/apiService.js";
import { ElMessage } from 'element-plus';
import { useStore } from "vuex";

const route = useRoute()
const router = useRouter()
let fianId = ref();
const show = ref(false)
const form = ref({
  realName: "",
  phone: "",
  idNum: "",
  combinationName1: "",
  combinationName2: "",
  combinationPhone1: "",
  combinationPhone2: "",
  combinationIdnum1: "",
  combinationIdnum2: "",
  rate: "",
  money: "",
  repayment: "",
  status: 0,
  file: []
})

const changeTime = (time) => {
  return time?.slice(0, 10) || ''
}

const showInfo = () => {
  show.value = true
}

const closeInfo = () => {
  show.value = false
}

const handleApply = (val) => {
  form.value.status = val;
  reviseFinanceById()
    .then((res) => {
      if (res.flag == true) {
        ElMessage.success(res.message)
        router.push({ name: "financingList" })
      } else {
        ElMessage.error(res.message)
      }
    })
    .catch((err) => {
      console.log(err)
      ElMessage.error(err.message)
    })
}

const getData = () => {
  getFinanceById()
    .then((res) => {
      form.value=res.data;
    })
    .catch((err) => {
      console.log(err)
    })
}

onMounted(() => {
  fianId.value = route.query.FianId;
  getData()
})

const getFinanceById = async()=>{
    const response = await apiClient.get(`finance/selectById/${fianId.value}`, {
          headers: {
            Authorization: window.localStorage.token,
          },
        });
    return response;
}

const reviseFinanceById = async()=>{
    const response = await apiClient.put(`/finance/update`, form.value, {
      headers: {
        Authorization: window.localStorage.token,
      },
    });
    return response;
}
</script>
